<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>通过While循环获取所有的父级节点</title>
</head>
<body>
    <div>
        <ul>
            <li>
                <span></span>
            </li>
        </ul>
    </div>
    <script>
        const span = document.querySelector('span');
        function getParentNodes(node){
            const nodes = [];
            while(node = node.parentNode){
                nodes.push(node);
            }
            return nodes;
        }

        console.log(getParentNodes(span));
    </script>
</body>
</html>